<?php

$func_name = "field_find.php";
log_message('[' . $func_name . ']' . LOG_PING);

if ((isset($_GET['type'])) && (isset($_GET['term']))) {
    $serach_type = $_GET['type'];
    $serach_term = $_GET['term'];
    log_message('[' . $func_name . ']' . SEARCH_FOR . "$serach_term / $serach_type");

    $field_types = get_field_types();

    foreach ($field_types as $type) {
        if ($type['field_name'] == $serach_type) {

            $search_result = find_field_input($serach_type, $serach_term);
            log_message('[' . $func_name . ']' . "Anzahl Treffer: " . count($search_result));
            
            $result = array();
            foreach ($search_result as $search_row) {
                $result[] = utf8_encode($search_row['field_value']);
                
            }
            $to_tpl = array();
            $to_tpl["filename"] = 'output_json.tpl';
            $to_tpl["data"] = array();
            $to_tpl['data'] = $result;
            unset($func_name);
            return $to_tpl;
        }
    }
    log_message('[' . $func_name . ']' . "Feld Typ nicht gefunden! " . $type['field_name']);
} else {
    log_message('[' . $func_name . ']' . "Kein GET Parameter erhalten!");
}
$to_tpl = array();
$to_tpl["filename"] = 'output_json.tpl';
$to_tpl["data"] = array();
$to_tpl['data']['result'] = false;
unset($func_name);
return $to_tpl;
?>
